﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;
using System.Threading;


namespace OpcSelfHost.printer
{
    public class SocketClient
    {

        String strIp = "192.168.3.14";//IP地址
        int iPort = 9100;//端口
        int TimeOut = 1000;//超时时间 毫秒
        private Socket sock = null;
        private bool bConn = false;//连接状态

        static SocketClient instance = null;
        private static readonly object padlock = new object();

        public SocketClient()
        {
        }

        public static SocketClient Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (padlock)
                    {
                        if (instance == null)
                        {
                            instance = new SocketClient();
                        }
                    }
                }
                return instance;
            }
        }


        public bool getIsConnected()
        {


            return bConn;

        }


        private void setConnected(bool bConn)
        {
            this.bConn = bConn;

        }

        public void setIpAndPort(String strIp, int iPort)
        {

            this.strIp = strIp;
            this.iPort = iPort;

        }
        private delegate string ConnectSocketDelegate(IPEndPoint ipep, Socket sock);

        private string ConnectSocket(IPEndPoint ipep, Socket sock)
        {
            string exmessage = "";
            try
            {
                sock.Connect(ipep);
            }
            catch (System.Exception ex)
            {
                exmessage = ex.Message;
            }
            finally
            {
            }

            return exmessage;
        }
        public bool ConnetToServices(String strIp, int iPort)
        {

            IPAddress ip = IPAddress.Parse(strIp);
            IPEndPoint ipep = new IPEndPoint(ip, iPort);//IP和端口
            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            ConnectSocketDelegate connect = ConnectSocket;
            IAsyncResult asyncResult = connect.BeginInvoke(ipep, sock, null, null);

            bool connectSuccess = asyncResult.AsyncWaitHandle.WaitOne(TimeOut, false);
            if (!connectSuccess)
            {
                MessageBox.Show(string.Format("连接失败！错误信息：{0}", "连接超时"));
                setConnected(false);
                return false;
            }

            string exmessage = connect.EndInvoke(asyncResult);
            if (!string.IsNullOrEmpty(exmessage))
            {
                MessageBox.Show(string.Format("连接失败！错误信息：{0}", exmessage));
                setConnected(false);
                return false;
            }

            setConnected(true);




            return true;

        }
        public bool ConnetToServices()
        {


            return ConnetToServices(this.strIp, this.iPort);

        }



        public void WriteFile(byte[] data)
        {


            if (sock != null)
            {


                try
                {

                    sock.Send(data);
                }
                catch (Exception ex)
                {

                    Console.WriteLine(ex);

                }

            }

        }

        public void CloseConnection()
        {


            setConnected(false);

            if (sock != null)
            {

                sock.Close();
            }





        }




    }
}
